﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Data.SqlClient;
using System.Collections.Generic;
using Library;


namespace BusinessLayer
{
   public class Product
    {
#region "Variable and Properties"

//Variables
        private string id;
        private string category_ID;     
        private string product_Name;       
        private int producer_ID;     
        private int price;        
        private int bao_hanh;        
        private int stock;       
        private string image_Url;
        private string detail;       
        private int del_Status;
        
//Properties
        public string Id
        {
            get { return id; }
            set { id = value; }
        }
        public string Category_ID
        {
            get { return category_ID; }
            set { category_ID = value; }
        }
        public string Product_Name
        {
            get { return product_Name; }
            set { product_Name = value; }
        }
        public int Producer_ID
        {
            get { return producer_ID; }
            set { producer_ID = value; }
        }
        public int Price
        {
            get { return price; }
            set { price = value; }
        }
        public int Bao_hanh
        {
            get { return bao_hanh; }
            set { bao_hanh = value; }
        }
        public int Stock
        {
            get { return stock; }
            set { stock = value; }
        }
        public string Image_Url
        {
            get { return image_Url; }
            set { image_Url = value; }
        }
        public string Detail
        {
            get { return detail; }
            set { detail = value; }
        }
        public int Del_Status
        {
            get { return del_Status; }
            set { del_Status = value; }
        }
#endregion
#region "Initial Methods"
        public Product() { }

        public Product(SqlDataReader sdr)
        {
            if (sdr != null && !sdr.IsClosed)
            {
                id = sdr.GetString(0);
                if (!sdr.IsDBNull(1))
                    category_ID = sdr.GetString(1);
                if (!sdr.IsDBNull(2))
                    product_Name = sdr.GetString(2);
                if (!sdr.IsDBNull(3))
                    producer_ID = sdr.GetInt32(3);
                if (!sdr.IsDBNull(4))
                    price = sdr.GetInt32(4);
                if (!sdr.IsDBNull(5))
                    bao_hanh = sdr.GetInt32(5);
                if (!sdr.IsDBNull(6))
                    stock = sdr.GetInt32(6);
                if (!sdr.IsDBNull(7))
                    image_Url = sdr.GetString(7);
                if (!sdr.IsDBNull(8))
                    detail = sdr.GetString(8);
                if (!sdr.IsDBNull(9))
                    del_Status = sdr.GetInt32(9);
            }
        }
#endregion
#region "Business Methods"
//Get all Products
        public static List<Product> GetAll()
        {
            DBHelp dbh = new DBHelp();
            List<Product> lstProduct = new List<Product>();
            string cmd = "tbl_Product_SHOWALL";
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            while (sdr.Read())
            {
                lstProduct.Add(new Product(sdr));
            }
            return lstProduct;
        }
//Get a Product
        public static Product GetOne(string id)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@ID", id);
            Product product = new Product();
            string cmd = "tbl_Product_SHOWONE";
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            if(sdr.Read())
            {
                product.Id = sdr.GetString(0);
                if (!sdr.IsDBNull(1))
                    product.Category_ID = sdr.GetString(1);
                if (!sdr.IsDBNull(2))
                    product.Product_Name = sdr.GetString(2);
                if (!sdr.IsDBNull(3))
                    product.Producer_ID = sdr.GetInt32(3);
                if (!sdr.IsDBNull(4))
                    product.Price = sdr.GetInt32(4);
                if (!sdr.IsDBNull(5))
                    product.Bao_hanh = sdr.GetInt32(5);
                if (!sdr.IsDBNull(6))
                    product.Stock = sdr.GetInt32(6);
                if (!sdr.IsDBNull(7))
                    product.Image_Url = sdr.GetString(7);
                if (!sdr.IsDBNull(8))
                    product.Detail = sdr.GetString(8);
                if (!sdr.IsDBNull(9))
                    product.Del_Status = sdr.GetInt32(9);
            }
            return product;
        }
//Get Product by Category
        public static List<Product> GetByCategory(string catID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@Category_ID", catID);
            List<Product> lstProduct = new List<Product>();
            string cmd = "tbl_Product_SHOWBYCATEGORY";
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            while (sdr.Read())
            {
                lstProduct.Add(new Product(sdr));
            }
            return lstProduct;
        }
//Get Product between Price
        public static List<Product> GetByPrice(int min, int max)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@Minprice", min);
            dbh.AddParameter("@Maxprice", max);
            List<Product> lstProduct = new List<Product>();
            string cmd = "tbl_Product_SHOWBYPRICE";
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            while (sdr.Read())
            {
                lstProduct.Add(new Product(sdr));
            }
            return lstProduct;
        }
//Get Product by Producer
        public static List<Product> GetByPrice(int producerID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@Producer_Id", producerID);
            List<Product> lstProduct = new List<Product>();
            string cmd = "tbl_Product_SHOWBYPRODUCER";
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            while (sdr.Read())
            {
                lstProduct.Add(new Product(sdr));
            }
            return lstProduct;
        }
//Insert Product
        public static void Insert(Product newProduct)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@ID", newProduct.Id);
            dbh.AddParameter("@Category_ID", newProduct.Category_ID);
            dbh.AddParameter("@Product_Name", newProduct.Product_Name);
            dbh.AddParameter("@Producer_Id", newProduct.Producer_ID);
            dbh.AddParameter("@Price", newProduct.Price);
            dbh.AddParameter("@Bao_hanh", newProduct.Bao_hanh);
            dbh.AddParameter("@Stock", newProduct.Stock);
            dbh.AddParameter("@Image_url", newProduct.Image_Url);
            dbh.AddParameter("@Detail", newProduct.Detail);
            string cmd = "tbl_Product_INSERT";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
//Update Product
        public static void Update(Product modifyProduct)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@ID", modifyProduct.Id);
            dbh.AddParameter("@Category_ID", modifyProduct.Category_ID);
            dbh.AddParameter("@Product_Name", modifyProduct.Product_Name);
            dbh.AddParameter("@Producer_Id", modifyProduct.Producer_ID);
            dbh.AddParameter("@Price", modifyProduct.Price);
            dbh.AddParameter("@Bao_hanh", modifyProduct.Bao_hanh);
            dbh.AddParameter("@Stock", modifyProduct.Stock);
            dbh.AddParameter("@Image_url", modifyProduct.Image_Url);
            dbh.AddParameter("@Detail", modifyProduct.Detail);
            string cmd = "tbl_Product_UPDATE";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
//Delete Product
        public static void Delete(string ID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@ID", ID);
            string cmd = "tbl_Product_DELETE";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
#endregion
    }
}
